package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品 控制层
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result add(@RequestBody DishDTO dishDTO){
        log.info("新增菜品信息：{}", dishDTO);
        dishService.addDish(dishDTO);

        //清除redis缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        deleteRedisData(key);

        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> findPageDish(DishPageQueryDTO dishPageQueryDTO){
        log.info("前端传入的分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.findPageDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    //TODO 此处需加@RequestParam注解，否则springMVC无法自动解析前端传过来的String参数: 1,2,3...
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品：{}", ids);
        dishService.deleteBatch(ids);

        //删除redis缓存数据
        deleteRedisData("dish_*");

        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> findDishById(@PathVariable Long id){
        DishVO dishVO = dishService.selectDishAndFlavorById(id);
        return Result.success(dishVO);
    }

    /**
     * 根据菜品id修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateById(@RequestBody DishDTO dishDTO){
        dishService.updateById(dishDTO);

        //删除redis缓存数据
        deleteRedisData("dish_*");

        return Result.success();
    }

    /**
     * 批量起售、停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售、停售")
    public Result updateStatusById(@PathVariable Integer status, Long id){
        dishService.updateStatusById(status, id);

        //删除redis缓存数据
        deleteRedisData("dish_*");

        return Result.success();
    }

    /**
     * 单个/批量删除菜品redis缓存数据
     * @param pattern
     */
    private void deleteRedisData(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
